/*
 * @Author: wangdazhuang
 * @Date: 2024-10-19 09:25:19
 * @LastEditTime: 2025-02-18 11:31:17
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/http/http_req_interceptor.dart
 */

import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import '../services/storage_service.dart';
import '../env/environment_service.dart';
import 'http_const.dart';

////请求拦截器
abstract class HttpReqInterceptor {
  static void Function(RequestOptions, RequestInterceptorHandler) onRequest =
      (options, handler) {
    final storageService = Get.find<StorageService>();
    if (storageService.token != null) {
      options.headers["aut"] = storageService.token;
    }
    final t = DateTime.now().millisecondsSinceEpoch.toString();
    options.headers["t"] = t;
    options.headers["s"] =
        md5.convert(utf8.encode(t.substring(3, 8))).toString();
    options.headers["User-Mark"] = Environment.appName;
    options.headers[Headers.acceptHeader] =
        HttpConst.kContentTypeApplicationJSON;
    if (!kIsWeb) {
      options.headers["User-Agent"] =
          GetPlatform.isAndroid ? HttpConst.kAndroidUA : HttpConst.kiOSUA;
    }
    options.headers["deviceId"] = storageService.deviceId;
    if (options.method.toUpperCase() == 'POST' && options.data == null) {
      options.data = {};
    }
    return handler.next(options);
  };
}
